import {TableItemConfig} from './TableType'
import typeOfUtils from "../../utils/type-of-utils";

class RenderUtils {

	static scopedSlots = ['default', 'header']

	/**
	 *
	 * @param vm
	 * @param createElement
	 * @param tableItems {TableItemConfig[]}
	 * @return {VNode[]}
	 */
	static children(vm, createElement, tableItems:TableItemConfig[]) {
		let result = []
		tableItems.forEach(i => {
      let children = []
			if (typeOfUtils.isArray(i.children)) {
				children.push(...RenderUtils.children(vm, createElement, i.children))
			}
      if(i.hidden){
        return
      }
			let config = {
				props: {
					...i
				},
        scopedSlots: undefined
      }
			let scopedSlots = {}
			RenderUtils.scopedSlots.forEach(slot => {
				// 支持模板 插槽名称优先使用slotName属性定义
        let slotName = i.slotName || i.prop
				if (vm.$scopedSlots[slotName + '_' + slot]) {
					scopedSlots[slot] = (props) => {
						return vm.$scopedSlots[slotName + '_' + slot]({item: i, props})
					}
				}
			})
			// 支持函数
			if (typeOfUtils.isFunction(i.typeSlots)) {
				let slots = i.typeSlots(createElement, i)
				scopedSlots = {
					...scopedSlots,
					...slots
				}
			}
			if (typeOfUtils.isNotEmpty(scopedSlots)) {
				config.scopedSlots = scopedSlots
			}
			// 定义item的插槽 prop+default
			result.push(createElement('el-table-column', config, children))
		})
		return result;
	}

	/**
	 *
	 * @param vm
	 * @return {*[]}
	 */
	static slots(vm) {
		let result = []
		if (vm.$scopedSlots.append) {
			result.push(...vm.$scopedSlots.append())
		}
		return result
	}

	/**
	 * 处理选择
	 * @param vm
	 * @param createElement
	 * @return {*[]}
	 */
	static select(vm, createElement) {
		let result = []
		if (vm.select) {
			if (vm.selectMultiple) {
				const item = createElement('el-table-column', {
					props: {
						...vm.selectConfig,
						type: 'selection',
						selectable: (row,index) => {
							if (typeOfUtils.isFunction(vm.selectConfig?.selectable)) {
								return vm.selectConfig.selectable(row,index) || false
							}
							return true
						}
					}
				})
				result.push(item)
			} else {
				const item = createElement('el-table-column', {
					props: {
						width: '50px',
						...vm.selectConfig,
					},
					scopedSlots:{
						default:(props)=>{
              let disabled = false
							if (typeOfUtils.isFunction(vm.selectConfig?.selectable)) {
								disabled = !vm.selectConfig?.selectable(props.row, props.$index) || false
							}
              props.row.__zhixin$index = props.$index
							const radio = createElement('el-radio',{
								props: {
									value: vm.zhixinRadio,
									label: props.$index,
									disabled
								},
								class: {
									zhixin_radio_not_show_label: true
								},
								nativeOn: { // 直接监听元素点击事件
									click: (event) => {
										if ('INPUT' === event.target.tagName.toUpperCase()) { // 这样就执行一次，需要执行一次rowClick
											vm._rowClick(props.row, props.column, event)
										}
										event.stopPropagation()
									}
								}
							})
							return [
								radio
							]
						}
					}
				})
				result.push(item)
			}
		}
		return result;
	}

  /**
   * 显示序号
   * @param vm
   * @param createElement
   * @return {*[]}
   */
  static showIndex(vm, createElement) {
    let result = []
    if (vm.showIndex) {
      const item = createElement('el-table-column', {
        props: {
          index: (index) => {
            if (vm.indexConfig?.indexFirstNumber) {
              return index + 1 + vm.indexConfig.indexFirstNumber
            }
            return index + 1
          },
          label:'序号',
          ...vm.indexConfig,
          type: 'index',
        }
      })
      result.push(item)
    }
    return result
  }
}

export default RenderUtils
